package com.paomi.client.fragment.collection;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.makeramen.roundedimageview.RoundedImageView;
import com.paomi.client.GlideApp;
import com.paomi.client.R;
import com.paomi.client.activity.detail.DetailTicketActivity;
import com.paomi.client.base.BaseLCEFragment;
import com.paomi.client.bean.BaseJSON;
import com.paomi.client.bean.CollectionListInfo;
import com.paomi.client.bean.CpllectionInfo;
import com.paomi.client.fragment.collection.presenter.CollectShowPresenter;
import com.paomi.client.utils.ToastUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by pc on 2019/1/29
 * Describe:收藏的演出列表
 * com.paomi.client.fragment.collection
 */
public class CollectShowFragment extends BaseLCEFragment<CpllectionInfo, CollectShowPresenter> {
    @BindView(R.id.list_collect)
    RecyclerView listCollect;
    private int pageNum=1;

    @NonNull
    @Override
    protected int getFragmentLayoutId() {
        return R.layout.fragment_recycler;
    }

    @NonNull
    @Override
    protected RecyclerView createRecycler() {
        return listCollect;
    }

    @Override
    protected int getItemLayout() {
        return R.layout.item_collection;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        getPresenter().setCollectData(getActivity(),"0",pageNum);
    }

    @Override
    protected void initEmpty(TextView empty) {
        empty.setTextSize(15);
        empty.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.img_collection_empty, 0, 0);
        empty.setText(getResources().getString(R.string.empty_collection));
    }

    @Override
    protected void convert(BaseViewHolder helper, CpllectionInfo item) {
        RoundedImageView perform_iv=helper.itemView.findViewById(R.id.perform_iv);
        GlideApp.with(this).asBitmap().load(item.getImg()).placeholder(R.mipmap.img_default_goods).into(perform_iv);
        helper.setText(R.id.tv_title,item.getName());
//            holder.tv_data.setText(new SimpleDateFormat("yyyy-MM-dd").format(info.getCreateTime()*1000));
        helper.setText(R.id.tv_data,item.getGoodsShowDate());
        helper.setText(R.id.tv_point,item.getGoodsVenueName());
        helper.setText(R.id.tv_money,"￥" + item.getGoodsPrice());
        switch (item.getTicketState()){//0是即将开票，1是预售中，2售票中，3演出结束，4已售罄，5扫尾票
            case 0://即将开票
                helper.setText(R.id.tv_btn, getResources().getString(R.string.ticket_action));
                helper.setTextColor(R.id.tv_btn,getResources().getColor(R.color.color_cbcbcb));
                helper.setBackgroundRes(R.id.tv_btn,R.drawable.bg_oreder_list_btn_gray);
                break;
            case 1:
                helper.setText(R.id.tv_btn, getResources().getString(R.string.ticketing_begin));
                helper.setTextColor(R.id.tv_btn,getResources().getColor(R.color.color_F01F07));
                helper.setBackgroundRes(R.id.tv_btn,R.drawable.bg_oreder_list_btn_f01f07);
                break;
            case 2:
                helper.setText(R.id.tv_btn, getResources().getString(R.string.ticketing));
                helper.setTextColor(R.id.tv_btn,getResources().getColor(R.color.color_F01F07));
                helper.setBackgroundRes(R.id.tv_btn,R.drawable.bg_oreder_list_btn_f01f07);
                break;
            case 3://演出结束
                helper.setText(R.id.tv_btn, getResources().getString(R.string.ticket_un));
                helper.setTextColor(R.id.tv_btn,getResources().getColor(R.color.color_cbcbcb));
                helper.setBackgroundRes(R.id.tv_btn,R.drawable.bg_oreder_list_btn_gray);
                break;
            case 4:
                helper.setText(R.id.tv_btn, getResources().getString(R.string.ticket_finish));
                helper.setTextColor(R.id.tv_btn,getResources().getColor(R.color.color_cbcbcb));
                helper.setBackgroundRes(R.id.tv_btn,R.drawable.bg_oreder_list_btn_gray);
                break;
            case 5://扫尾票
                helper.setText(R.id.tv_btn, getResources().getString(R.string.ticket_none));
                helper.setTextColor(R.id.tv_btn,getResources().getColor(R.color.color_F01F07));
                helper.setBackgroundRes(R.id.tv_btn,R.drawable.bg_oreder_list_btn_f01f07);
                break;
        }

        helper.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
                builder.setMessage("是否删除");
                builder.setNegativeButton("取消", null);
                builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Map<String, String> map = new HashMap<>();
                        map.put("typeId", ""+item.getTypeId());
                        map.put("type", "0");
                        getPresenter().removeCollection(map, helper.getLayoutPosition());
                    }
                });
                builder.show();
                return false;
            }
        });

    }

    @Override
    protected void onItemClick(View view, CpllectionInfo item) {
        Intent intent = new Intent(getActivity(), DetailTicketActivity.class);
        intent.putExtra("getId", "" + item.getTypeId());
        startActivity(intent);
    }


    @NonNull
    @Override
    public CollectShowPresenter createPresenter() {
        return new CollectShowPresenter();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().unregister(this);
        }
    }

    @Override
    public void setData(List<CpllectionInfo> data, boolean pullToRefresh) {
        super.setData(data, pullToRefresh);
        if(getmAdapterWrapper()!=null){
            getmAdapterWrapper().setLoadItemVisibility(pullToRefresh);
            getmLoadMoreHelper().setLoadMoreFinish();
            getmAdapterWrapper().notifyDataSetChanged();
        }else{
            getAdapter().notifyDataSetChanged();
        }
    }

    @Override
    public void onLoad() {
        super.onLoad();
        pageNum++;
        getPresenter().setCollectData(getActivity(),"0",pageNum);
    }

    /**
     * 取消收藏
     * @param myError
     */
    @Subscribe
    public void onEvent(BaseJSON myError) {

    }
}
